<?php
namespace dg\Controller;
use Think\Controller;
use Think\Hook;
use Think\Think;
class ApibaseController extends Controller
{
    /*
	 * 分消商城_基类初始化
	 * @date: 2016-3-16 下午2:58:39
	 * @editor: XING
	 */
	public function __construct(){
        Hook::listen('action_begin',$this->config);
		//实例化视图类
		$this->view     = Think::instance('Think\View');
		//控制器初始化
		if(method_exists($this,'_initialize'))
			$this->_initialize();
	    //登陆状态验证
        if(!is_login() && !in_array(get_class($this),C('FiterControl'))){
        	redirect('/user/login');
        }

        //如果是超级管理员 不需要进行权限验证
        if(session('loginUid') && session('role_id') != 1 && ACTION_NAME != 'login' && ACTION_NAME!='checklogin' && ACTION_NAME != 'loginout'){
            //权限验证
            $Auth = new \Think\Auth();
            //需要验证的规则列表,支持逗号分隔的权限规则或索引数组
            $name = '/'.CONTROLLER_NAME . '/' . ACTION_NAME;
            //当前用户id
            $uid = session('loginUid');
            //分类
            $type = CONTROLLER_NAME;
            //执行check的模式
            $mode = 'url';
            $relation = 'or';
            if (!$Auth->check($name, $uid, $type, $mode, $relation)) {
                $this->assign('waitSecond',2);
                $this->error('没有权限！');
            }
        }
        $this->auth();

	}

	private function auth() {
        //没权限的模块不显示
        if(session('loginUid') && session('loginUid') != 1){
            $access = M('AuthGroupAccess g')->join('LEFT JOIN qxww_access a ON g.role_id = a.role_id')->where(array('uid'=>session('loginUid')))->find();
            $nodes = $access['node_id'];
            $nodes_arr = M('Node')->where(array('id'=>array('in',$nodes),'pid'=>0))->field('id,pid,name as link,title as tip')->select();
            //找到父级 按父级进行分类 是菜单的
            foreach($nodes_arr as $k=>$val){
                $arr = M('Node')->where(array('id'=>array('in',$nodes),'pid'=>$val['id']))->field('name link,title tip')->select();
                $menus[] = array_values(array_merge_recursive(array($val),$arr));
            }
        }else{
            $menus = C('menus');
        }
        $this->assign('menus', $menus);
    }
}